﻿using UnityEngine;
using System.Collections;

public class SequencePlayAnimationComponent :MonoBehaviour
{
	public BakedAnimationPlayer player;

	public string[] animationQueue;

	public float cyclePerAction = 6;

	private float _currentTime = 0;
	private float _currentTotalTime = 0;
	private int _currentPlayerIndex = -1;
	// Use this for initialization
	void Start()
	{
		if (animationQueue.Length > 0)
		{
			SwitchAnimationTo(0);
		}
	}

	void SwitchAnimationTo(int index)
	{
		index %= animationQueue.Length;
		_currentPlayerIndex = index;
		_currentTime = 0;
		player.Play(animationQueue[index]);
		_currentTotalTime = player.GetRuntimeClipLength(animationQueue[index]) * cyclePerAction;
	}

	// Update is called once per frame
	void Update()
	{
		if (this.enabled && player && player.enabled)
		{
			if (_currentTime >= _currentTotalTime)
			{
				SwitchAnimationTo(_currentPlayerIndex + 1);
				return;
			}
			_currentTime += Time.smoothDeltaTime;
		}
	}
}
